/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


// testpgl.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "testpgl.h"

#include "MainFrm.h"
#include "testpglDoc.h"
#include "testpglView.h"
#include "PGLDemo.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTestpglApp

BEGIN_MESSAGE_MAP(CTestpglApp, CWinApp)
	//{{AFX_MSG_MAP(CTestpglApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestpglApp construction

CTestpglApp::CTestpglApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
	m_pIdleView = NULL;
	m_pScatterDlg = NULL;
	m_pScatter = NULL;
	m_pAnimatedLine.reset();
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CTestpglApp object

CTestpglApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CTestpglApp initialization

BOOL CTestpglApp::InitInstance()
{
	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

	// Change the registry key under which our settings are stored.
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	AfxEnableControlContainer();
	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CTestpglDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CTestpglView));
	AddDocTemplate(pDocTemplate);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

#ifndef PGL_NO_NEWMENU
	// Set drawing style of all menus to XP-Mode or STYLE_ORIGINAL
	CNewMenu::SetMenuDrawMode(CNewMenu::STYLE_XP);
#endif

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	// The one and only window has been initialized, so show and update it.
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();

	// initialize gdi+
	m_GDIpInitializer.Initialize();

	return TRUE;
}


CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
	// TODO
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CTestpglApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CTestpglApp message handlers

BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
 

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

int CAboutDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	  
	return 0;
}

int CTestpglApp::ExitInstance() 
{
	// shutdown GDI+
	m_GDIpInitializer.Deinitialize();
	
	return CWinApp::ExitInstance();
}

CPGLRegionPtr CTestpglApp::GenerateRegion()
{
	CPGLRegionPtr pRegion(new CPGLRegion());

	CPGLDemo::GetExampleSubplot(pRegion);

	return pRegion;
}

void CTestpglApp::AnimateLine()
{
	if (!m_pAnimatedLine)
		return;

	clock_t tNow = clock();
	double duration = (double)(tNow - m_tStart) / CLOCKS_PER_SEC;
	double x;

	// calcul
	for (UINT i=0;i<(UINT)m_pAnimatedLine->GetNPoints();i++)
	{
		x=(double)i/(double)m_pAnimatedLine->GetNPoints()*10;
//		m_pAnimatedLine->SetXi(i,  x);
//		m_pAnimatedLine->SetYi(i, (cos(duration*2+x)+sin(duration-x))/2.0);
	}
}

BOOL CTestpglApp::OnIdle(LONG lCount) 
{
	if (m_pIdleView)
	{
		if (m_pAnimatedLine)
			AnimateLine();

		m_pIdleView->InvalidateRect(NULL,FALSE);
		return TRUE;
	}


	return CWinApp::OnIdle(lCount);
}
